package ezonsport.android.data;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import ezonsport.android.data.DataHelper;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;

public class OrmliteDataContext {
	private OrmliteSession session;

	public OrmliteDataContext(DataHelper dataHelper) {
		session = new OrmliteSession(dataHelper);
	}

	// 增加
	public <T, ID> void add(T item, Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		session.save(item, dataClass, idClass);
	}

	// 删除
	public <T, ID> void delete(T item, Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		session.delete(item, dataClass, idClass);
	}

	// 更新
	public <T, ID> void update(T item, Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		session.update(item, dataClass, idClass);

	}

	// 根据ID查询
	public <T, ID, K extends ID> T queryById(Class<T> dataClass,
			Class<K> idClass, ID id) throws SQLException {
		return session.queryById(dataClass, idClass, id);
	}

	// 查询全部
	public <T, ID> List<T> queryForAll(Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		return session.queryForAll(dataClass, idClass);
	}

	// 根据条件查询
	public <T, ID> List<String[]> queryBySql(Class<T> dataClass, Class<ID> idClass,
			String query) throws SQLException {
		return session.queryBySql(dataClass, idClass, query);
	}

	// 获取全部记录数
	public <T, ID> long countof(Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		return session.countof(dataClass, idClass);
	}

	// 获取符合条件的记录数
	public <T, ID> long countof(Class<T> dataClass, Class<ID> idClass,
			PreparedQuery<T> query) throws SQLException {
		return session.countof(dataClass, idClass, query);
	}
	
	/**
	 * 通用查询
	 * @param <T>
	 * @param <ID>
	 * @param dataClass
	 * @param idClass
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	public <T, ID> List<T> query(Class<T> dataClass, Class<ID> idClass,
			PreparedQuery<T> query)throws SQLException
	{
		return session.query(dataClass, idClass, query);
	}
	
	/**
	 * 获取QueryBuilder
	 * @param <T>
	 * @param <ID>
	 * @param dataClass
	 * @param idClass
	 * @return
	 * @throws SQLException
	 */
	public <T, ID> QueryBuilder<T,ID> getQueryBuilder(Class<T> dataClass, Class<ID> idClass)throws SQLException
	{
		
		return session.getQueryBuilder(dataClass, idClass);
	}
	
	
//	//获取指定时间段的运动记录
//	public <T, ID> List<String[]> queryByDate(Class<T> dataClass, Class<ID> idClass,
//			Date from, Date to) throws SQLException {
//		return session.queryByDate(dataClass, idClass, from, to);
//	}

	/**
	 * 开始事务
	 */
	public void beginTransaction() {
		session.beginTransaction();
	}

	//提交事务
	public void commit() {
		session.commit();
	}

	//回滚事务
	public void rollback() {
		session.rollback();
	}
}
